//TESH.scrollpos=91
//TESH.alwaysfold=0
library PropertySystem initializer Init requires Database

    globals
        private unitstate STATE_UNIT_DMGPLUS = ConvertUnitState(0x12)//基础攻击 0x12 
        private unitstate STATE_UNIT_SIDES = ConvertUnitState(0x11)//攻击筛子面数 0x11
        private unitstate STATE_UNIT_DICE = ConvertUnitState(0x10)//攻击骰子数量 0x10 
        private unitstate STATE_UNIT_SUBJOIN = ConvertUnitState(0x13)// 附加攻击 0x13
        private hashtable slk_database = InitHashtable()
        private constant string TAG_PROPERT = "Propert."
    endglobals

    private function Init takes nothing returns nothing
        
    endfunction
    
    // 获取和设置单位属性
    //! textmacro GetAndSetUnitBasePropertyHandler takes name, type
    function GetPropertyAt$name$ takes handle h, string tag returns $type$
        return Get$name$("entity",GetHandleId(h),TAG_PROPERT+tag)
    endfunction

    function SetPropertyAt$name$ takes handle h, string tag, $type$ value returns nothing
        call Set$name$("entity",GetHandleId(h),TAG_PROPERT+tag, value)
    endfunction
    //! endtextmacro
    //! runtextmacro GetAndSetUnitBasePropertyHandler("Int","integer")
    //! runtextmacro GetAndSetUnitBasePropertyHandler("Real","real")
    //! runtextmacro GetAndSetUnitBasePropertyHandler("Str","string")
    
    function GetDropRate takes handle h returns real
        return GetPropertyAtReal(h, "DropRate")
    endfunction

    function SetDropRate takes handle h, real value returns nothing
        call SetPropertyAtReal(h,"DropRate",value)
    endfunction
    
    function AddDropRate takes handle h, real value returns nothing
        call SetPropertyAtReal(h,"DropRate",GetDropRate(h)+value)
    endfunction
    
    function GetCritOdds takes handle h returns real
        return 0.06 + GetPropertyAtReal(h, "CritOdds")
    endfunction

    function SetCritOdds takes handle h, real value returns nothing
        call SetPropertyAtReal(h,"CritOdds",value)
    endfunction
    
    function AddCritOdds takes handle h, real value returns nothing
        call SetPropertyAtReal(h,"CritOdds",GetCritOdds(h) + value)
    endfunction
    
    function GetCritDamage takes handle h returns real
        return 1.50 + GetPropertyAtReal(h, "CritDamage")
    endfunction

    function SetCritDamage takes handle h, real value returns nothing
        call SetPropertyAtReal(h,"CritDamage",value)
    endfunction
    
    function AddCritDamage takes handle h, real value returns nothing
        call SetPropertyAtReal(h,"CritDamage",GetCritDamage(h)+value)
    endfunction
    
    // 基础攻击 
    function GetUnitAttackBase takes unit u returns real
        return GetUnitState(u,STATE_UNIT_DMGPLUS)
    endfunction 
    
    // 基础攻击 
    function SetUnitAttackBase takes unit u,real value returns nothing
        call SetUnitState(u,STATE_UNIT_DMGPLUS, value)
    endfunction 
    
    // 基础攻击 
    function AddUnitAttackBase takes unit u,real value returns nothing
        call SetUnitAttackBase(u,GetUnitAttackBase(u) + value)
    endfunction
    
    // 攻击筛子面数 
    function GetUnitAttackSides takes unit u returns real
        return GetUnitState(u,STATE_UNIT_SIDES)
    endfunction 
    
    // 攻击筛子面数 
    function SetUnitAttackSides takes unit u,real value returns nothing
        call SetUnitState(u,STATE_UNIT_SIDES, value)
    endfunction 
    
    // 攻击筛子面数 
    function AddUnitAttackSides takes unit u,real value returns nothing
        call SetUnitAttackSides(u,GetUnitAttackSides(u)+value)
    endfunction
    
    // 攻击骰子数量
    function GetUnitAttackDice takes unit u returns real
        return GetUnitState(u,STATE_UNIT_DICE)
    endfunction 
    
    // 攻击骰子数量
    function SetUnitAttackDice takes unit u,real value returns nothing
        call SetUnitState(u,STATE_UNIT_DICE, value)
    endfunction 
    
    // 攻击骰子数量
    function AddUnitAttackDice takes unit u,real value returns nothing
        call SetUnitAttackDice(u,GetUnitAttackDice(u)+value)
    endfunction
    
    // 附加攻击
    function GetUnitAttackSubjoin takes unit u returns real
        return GetUnitState(u,STATE_UNIT_SUBJOIN)
    endfunction 
    
    // 附加攻击
    function SetUnitAttackSubjoin takes unit u,real value returns nothing
        call SetUnitState(u,STATE_UNIT_SUBJOIN, value)
    endfunction 
    
    // 附加攻击
    function AddUnitAttackSubjoin takes unit u,real value returns nothing
        call SetUnitAttackSubjoin(u,GetUnitAttackSubjoin(u)+value)
    endfunction
    
    // 获得一个算计攻击值
    function GetRandomAttackValue takes unit u returns real
        local real atk = GetUnitAttackBase(u)
        local real dice = GetUnitAttackDice(u)
        set atk =atk + GetUnitAttackSubjoin(u)
        return GetRandomReal(atk + dice,atk + dice * GetUnitAttackSides(u))
    endfunction
    
    // 获取主属性
    function GetHeroMainProperty takes unit hero returns integer
        local string primary = "Primary"
        if IsUnitType(hero, UNIT_TYPE_HERO) then
            set primary = GetStr("config",GetUnitTypeId(hero),"Primary")
            if primary == "STR" then
                return GetHeroStr(hero, true)
            elseif primary == "AGI" then
                return GetHeroAgi(hero, true)
            elseif primary == "INT" then
                return GetHeroInt(hero, true)
            endif
        endif
        return 0
    endfunction

endlibrary 